#ifndef CUSTOM_RC_RENDER_INCLUDED
#define CUSTOM_RC_RENDER_INCLUDED

#include "RCCommon.hlsl"

// Uniform Parameter
uniform int Cascade_Level;                          // Current Cascade Level n
Texture2D _BlitTexture;

float4 RCRenderFragment(Vert2Frag Input) : SV_TARGET {
    ProbeInfo Probe = GetProbeInfo(Cascade_Level, Input.ScreenUV);

    // Find Current Pixel in which Probe
    float2 PixPos = Input.ScreenUV * _ScreenParams.xy + 0.5;
    float2 ProbePix         = max(float2(0, 0), PixPos - Probe.ProbeLength / 2);

    int2 ProbeIndex         = int2(ProbePix / Probe.ProbeLength);

    float4 RenderResult     = float4(0, 0, 0, 0);
    float4 LDResult = SampleCascade(_BlitTexture, ProbeIndex + int2(0, 0), Probe.AngleSize, 0, Probe.AngleSize.x * Probe.AngleSize.y);
    float4 RDResult = SampleCascade(_BlitTexture, ProbeIndex + int2(1, 0), Probe.AngleSize, 0, Probe.AngleSize.x * Probe.AngleSize.y);
    float4 LUResult = SampleCascade(_BlitTexture, ProbeIndex + int2(0, 1), Probe.AngleSize, 0, Probe.AngleSize.x * Probe.AngleSize.y);
    float4 RUResult = SampleCascade(_BlitTexture, ProbeIndex + int2(1, 1), Probe.AngleSize, 0, Probe.AngleSize.x * Probe.AngleSize.y);
    
    RenderResult = BilinearLerp(LDResult, RDResult, LUResult, RUResult, (ProbePix - Probe.ProbeLength * ProbeIndex) / Probe.ProbeLength);

    return RenderResult;
}
#endif